import java.util.Arrays;

public class MyStack<E> {
    public Object [] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE=10;
    public MyStack(){
         elem=new Object[DEFAULT_SIZE];
    }
    //入栈
    public void push(E val){
        //1.先判断栈是否满了
        if (isFull()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize]=val;
        usedSize++;
    }
    //出栈
    public E pop(){
        //1.先判断是否为空
        if (isEmpty()){
            return null;
        }
       E ret=(E)elem[usedSize-1];
        usedSize--;
        return ret;
    }
    //获取栈顶元素
    public E peek(){
        //1.先判断是否为空
        if (isEmpty()){
            return null;
        }
        E ret=(E)elem[usedSize-1];
        return ret;
    }
    private boolean isFull(){
        return usedSize== elem.length;
    }
    private boolean isEmpty(){
        return usedSize==0;
    }
}
